package com.gupao.chat.server;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@Configuration
@Slf4j
public class ChatServer {
    @Value("${server.ip.port}")
    private int port;
    private static ServerSocket serverSocket;
    private Socket socket;
    private String backMessage;

    private List<Socket> sockets = new ArrayList(10);

    @Bean
    public void startServer() {
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            while (true) {
                try {
                    socket = serverSocket.accept();
                    if (socket == null) {
                        log.error("socket获取错误");
                        return;
                    }
                    sockets.add(socket);
                    if (socket.isConnected()) {
                        readMessage(socket);
                        sendMessage();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void sendMessage() {
        new Thread(() -> {
            while (true) {
                Scanner scanner = new Scanner(System.in);
                backMessage = scanner.nextLine();
                for (Socket socket : sockets) {
                    DataOutputStream dos = null;
                    if (socket != null && socket.isConnected()) {////避免突然断掉客户端时报空指针异常
                        try {
                            dos = new DataOutputStream(socket.getOutputStream());
                            dos.writeUTF("server(3000):" + backMessage + "\n");
                            dos.flush();
                        } catch (IOException e) {
                            log.error(e.getMessage());
                            try {
                                socket.close();
                                socket = null;
                                scanner.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                            break;
                        }
                    }
                }
            }
        }).start();
    }

    private void readMessage(final Socket socket) {
        new Thread(() -> {
            while (socket.isConnected()) {
                String message;
                try {
                    message = new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine();
                    if (!StringUtils.isEmpty(message)) {
                        System.out.println(message);
                        System.out.println("请输入回执信息：");

                    }
                } catch (IOException e) {
                    log.error(e.getMessage());
                    try {
                        socket.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
